package com.ruike.alisurface.ui.settings;

import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.R;
 import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.SettingPageItemBean;
import com.ruike.alisurface.https.MchApiHttp;
import com.ruike.alisurface.ui.adapters.SettingPageAdapter;
import com.voodoo.lib_http.ApiListener;
import com.voodoo.lib_logcat.L;


import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class OpenDoor_Aty extends BaseAty implements SettingPageAdapter.OnSettingItemClickListener , ApiListener {


    @BindView(R.id.opendoor_recv)
    RecyclerView opendoorRecv;
    List<SettingPageItemBean> settingPageItemBeans;
    SettingPageAdapter adapter;
    @BindView(R.id.goback_imgv)
    ImageView gobackImgv;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_open_door;
    }

    @Override
    protected void initViews() {

        initTitleBar(false);
        gobackImgv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        settingPageItemBeans = new ArrayList<>();
        settingPageItemBeans.add(new SettingPageItemBean(1, R.mipmap.icon_door, getResources().getString(R.string.opendoor_one)));
        settingPageItemBeans.add(new SettingPageItemBean(2, R.mipmap.icon_door, getResources().getString(R.string.opendoor_twe)));
        settingPageItemBeans.add(new SettingPageItemBean(3, R.mipmap.icon_door, getResources().getString(R.string.opendoor_three)));
        settingPageItemBeans.add(new SettingPageItemBean(4, R.mipmap.icon_door, getResources().getString(R.string.opendoor_four)));
        settingPageItemBeans.add(new SettingPageItemBean(5, R.mipmap.icon_door, getResources().getString(R.string.opendoor_five)));
        adapter = new SettingPageAdapter(this, settingPageItemBeans);
        adapter.setOnSettingItemClickListener(this);
        opendoorRecv.setLayoutManager(new GridLayoutManager(this,   2));
        opendoorRecv.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Constant.isShopShipment = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        Constant.isShopShipment = false;
    }




    @Override
    public void onSettingItemClick(SettingPageItemBean settingPageItemBean, int index) {
        switch (settingPageItemBean.getFlag()) {
            case 1: //开启电控锁门1

                MchApiHttp.getDoorlock( this, "0");
                showRightToast(getString(R.string.open_door));
                break;
            case 2: //开启电控锁门2

                MchApiHttp.getDoorlock(this, "1");
                showRightToast(getString(R.string.open_door));

                break;
            case 3: // 开启电控锁门3

                MchApiHttp.getDoorlock(this, "2");
                showRightToast(getString(R.string.open_door));

                break;
            case 4: // 开启电控锁门4

                MchApiHttp.getDoorlock(this, "3");
                showRightToast(getString(R.string.open_door));

                break;
            case 5: // 开启电控锁门4

                MchApiHttp.getDoorlock(this, "4");
                showRightToast(getString(R.string.open_door));

                break;
            default:
                break;

        }
    }



    @Override
    public void onRequestStart(String requestUrl, int requestId) {

    }

    @Override
    public void onRequestComplete(String requestUrl, int requestId) {

    }

    @Override
    public void onSuccess(String requestUrl, JSONObject requestJson, int requestId) {
        if (requestUrl.endsWith("send_open_lock")) {
            L.i(" lock==  onSuccess ", requestJson);
//            showRightTip(getString(R.string.open_door));
        }
    }

    @Override
    public void onError(String requestUrl, String errorMsg, String resultData, int requestId) {
        if (requestUrl.endsWith("send_open_lock")) {

            L.i(" lock==  onError ", errorMsg);
//            showRightTip(getString(R.string.open_door_fail));

        }
    }
}
